/**
 * 
 */
package net.mikoo.seals.boss.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.boss.biz.manager.OssManager;
import net.mikoo.seals.boss.biz.services.GeoServices;
import net.mikoo.seals.boss.common.BaseController;
import net.mikoo.seals.boss.model.Geo;
import net.mikoo.seals.boss.model.GeoType;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * 
 * @Filename GeoController.java
 *
 * @Description
 *
 * @Version 1.0
 *
 * @Author huangxiaoming
 *
 * @Email huangxm129@163.com
 * 
 * @History
 *          <li>Author: huangxiaoming</li>
 *          <li>Date: 2014年12月4日</li>
 *          <li>Version: 1.0</li>
 *          <li>Content: create</li>
 *
 */
@Controller
@RequestMapping("/admin/geo")
public class GeoController extends BaseController {

	Logger logger = Logger.getLogger(GeoController.class);

	@Autowired
	private GeoServices geoServices;
	
	@Autowired
	private OssManager ossManager;

	@RequestMapping("/index")
	public String index(Long parentId,Model model) {
		if(parentId == null ){
			parentId = 0L;
		}
		List<Geo> geos = geoServices.loadByParentId(parentId);
		//Result<List<Geo>> result = geoServices.loadAsTree();
			model.addAttribute("geos", geos);
			return "geo/index";
	}

	/**
	 * 
	 * @Description 访问添加Geo表单
	 *
	 * @Version 1.0
	 *
	 * @Author huangxiaoming
	 *
	 * @Email huangxm129@163.com
	 *
	 * @History
	 *
	 *          <li>Author: huangxiaoming</li>
	 *          <li>Date: 2014年12月4日</li>
	 *          <li>Version: 1.0</li>
	 *          <li>Content: create</li>
	 *
	 * @return
	 */
	@RequestMapping("/form")
	public String form(Long parentId, Model model) {
		List<GeoType> geoTypes = geoServices.loadTypes();
		model.addAttribute("geoTypes", geoTypes);
		// 查询树状地区树结构
		List<Geo> geos = geoServices.loadAsTree();

		model.addAttribute("geos", geos);

		model.addAttribute("parentId", parentId);

		return "geo/form";
	}

	/**
	 * 
	 * @Description 提交创建Geo表单
	 *
	 * @Version 1.0
	 *
	 * @Author huangxiaoming
	 *
	 * @Email huangxm129@163.com
	 *
	 * @History
	 *
	 *          <li>Author: huangxiaoming</li>
	 *          <li>Date: 2014年12月4日</li>
	 *          <li>Version: 1.0</li>
	 *          <li>Content: create</li>
	 *
	 * @param Geo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/create")
	public void create(Geo geo, HttpServletRequest request, Model model) {

		geoServices.save(geo);
	}

	/**
	 * 
	 * @Description 访问编辑geo表单
	 *
	 * @Version 1.0
	 *
	 * @Author huangxiaoming
	 *
	 * @Email huangxm129@163.com
	 *
	 * @History
	 *
	 *          <li>Author: huangxiaoming</li>
	 *          <li>Date: 2014年12月4日</li>
	 *          <li>Version: 1.0</li>
	 *          <li>Content: create</li>
	 *
	 * @param geoId
	 * @param model
	 * @return
	 */
	@RequestMapping("/edit")
	public String edit(Long geoId, Model model) {
		Geo geo = geoServices.loadByPrimaryKey(geoId);
		model.addAttribute("geo", geo);

		List<GeoType> geoTypes = geoServices.loadTypes();
		model.addAttribute("geoTypes", geoTypes);

		// 查询树状地区树结构

		List<Geo> geos = geoServices.loadAsTree();
		model.addAttribute("geos", geos);

		return "geo/edit";
	}

	/**
	 * 
	 * @Description 修改提交Geo表单
	 *
	 * @Version 1.0
	 *
	 * @Author huangxiaoming
	 *
	 * @Email huangxm129@163.com
	 *
	 * @History
	 *
	 *          <li>Author: huangxiaoming</li>
	 *          <li>Date: 2014年12月4日</li>
	 *          <li>Version: 1.0</li>
	 *          <li>Content: create</li>
	 *
	 * @param geo
	 * @param model
	 * @return
	 */
	@RequestMapping("/modify")
	public void modify(Geo geo, Model model) {
		geoServices.modifyByPrimaryKey(geo);
	}

	/**
	 * 
	 * @Description 删除Geo
	 *
	 * @Version 1.0
	 *
	 * @Author huangxiaoming
	 *
	 * @Email huangxm129@163.com
	 *
	 * @History
	 *
	 *          <li>Author: huangxiaoming</li>
	 *          <li>Date: 2014年12月4日</li>
	 *          <li>Version: 1.0</li>
	 *          <li>Content: create</li>
	 *
	 * @param geoId
	 * @param model
	 * @return
	 */
	@RequestMapping("/delete")
	public void delete(Long geoId, Model model) {
		geoServices.deleteByPrimaryKey(geoId);
	}

	@RequestMapping("/loadChild")
	@ResponseBody
	public List<Map<String, Object>> loadChild(Long parentId, Model model) {
		List<Geo> geos = geoServices.loadByParentId(parentId);

		List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
		if (geos != null && !geos.isEmpty()) {
			for (Geo geo : geos) {
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("geoId", geo.getGeoId());
				data.put("geoName", geo.getGeoName());
				datas.add(data);
			}
		}
		return datas;
	}

	@RequestMapping("/batch")
	public String batch(Long parentId, Model model) {
		List<GeoType> geoTypes = geoServices.loadTypes();
		model.addAttribute("geoTypes", geoTypes);
		// 查询树状地区树结构
		List<Geo> geos = geoServices.loadAsTree();
		model.addAttribute("geos", geos);
		model.addAttribute("parentId", parentId);

		return "geo/batch";
	}

	@RequestMapping("/batchCreate")
	public String batchCreate(Geo geo, HttpServletRequest request, Model model) {

		// 区域ID|区域名称|区域代码|区域简称|邮政编码|区号|经度|纬度

		String batchDatas = geo.getBatchDatas();
		String lines[] = batchDatas.split("\\r?\\n");
		for (String string : lines) {
			String arrays[] = string.split("\\|");
			Long parentId = geo.getParentId();

			if (StringUtils.isNotBlank(arrays[0])) {
				Long geoId = Long.parseLong(arrays[0]);
				geo.setGeoId(geoId);
			}
			String geoName = arrays[1];
			// String geoCode = arrays[2];
			// String abbreviation = arrays[3];
			// String postalCode = arrays[4];
			// String telCode = arrays[5];
			geo.setParentId(parentId);
			geo.setGeoName(geoName);
			// addGeo.setGeoCode(geoCode);
			// addGeo.setAbbreviation(abbreviation);
			// addGeo.setPostalCode(postalCode);
			// addGeo.setTelCode(telCode);
			geoServices.save(geo);
		}

		return "add";
	}

	@RequestMapping("/loadGeoDatas")
	@ResponseBody
	public Map<String,Object> loadGeoDatas(HttpServletRequest request,HttpServletResponse response, Model model) throws IOException {
		JSONArray geos = geoServices.refreshCache();
		
		JSONObject json  = new JSONObject();
		json.put("code", 0);
		json.put("msg", "success");
		json.put("data", geos);
		
		ossManager.upload("asset/js/geo.json", json.toString());
		
		
		return json;
	}

}